<template>
  <!-- 登录表单页面 -->
  <div style="margin-top: 20px">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="auto"
      class="demo-ruleForm"
    >
      <el-form-item prop="phone">
        <el-input
          placeholder="请输入内容"
          v-model="ruleForm.phone"
          class="input-with-select"
        >
          <el-select
            v-model="select"
            slot="prepend"
            placeholder="+86"
            style="width: 80px"
          >
            <el-option label="+86" value="1"></el-option>
            <el-option label="+123" value="2"></el-option>
            <el-option label="+88" value="3"></el-option>
          </el-select>
        </el-input>
      </el-form-item>

      <el-form-item prop="verifycode">
        <el-input
          v-model="ruleForm.verifycode"
          class="input-with-select"
          placeholder="请输入验证码"
          style="background-color: white; box-sizing: border-box"
          id="elInput"
        >
          <el-button
            slot="append"
            id="el-button"
            :disabled="ruleForm.phone == '' || isTimer"
            @click="getVerifyCode"
          >
            <span v-if="!isTimer"> 获取验证码 </span>
            <span v-if="isTimer"> 已发送({{ timer }}秒) </span>
          </el-button>
        </el-input>
      </el-form-item>

      <div class="agreement" style="margin-top: 20px">
        <el-tooltip
          content="请阅读并勾选下方协议"
          placement="top-start"
          v-model="isChecked"
          manual
        >
          <el-checkbox v-model="checked"> </el-checkbox>
        </el-tooltip>

        <span class="agreement-text">
          我已阅读并同意
          <a href="/rule" target="_blank" class="agreement-text-link">
            《用户协议》
          </a>
          <a href="/rule" target="_blank" class="agreement-text-link">
            《隐私政策》
          </a>
        </span>
      </div>

      <el-form-item>
        <div class="submit-login">
          <span @click="submitForm('ruleForm')">立即登录</span>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "loginPWForm",
  data() {
    return {
      ruleForm: {
        //手机号和验证码
        phone: 13128913638,
        verifycode: "",
      },
      checked: false, //是否打勾
      isChecked: false, //判断是否需要弹出信息
      select: "",
      isTimer: false, //判断是否发送验证码
      timer: 2,
      time: "", //存储定时器
      /* 规则 */
      rules: {
        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        verifycode: [
          { required: true, message: "请输入验证码", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      const that = this;
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (this.checked == false) {
            this.isChecked = true;
          } else {
            that.$http.loginByPhoneAndVerifyCode(this.ruleForm).then((res) => {
              if (res.status == 200) {
                alert("登录成功");
              }
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getVerifyCode() {
      let that = this;
      that.$http.getPhoneVerifyCode(this.ruleForm).then((res) => {
        console.log(res);
        if (res.status == 200) {
          this.$message({
            message: "发送验证码成功",
            type: "success",
          });
          this.ruleForm.verifycode = 764188;
          this.isTimer = true;
          this.setTime();
        } else {
          this.$message.error("发送失败");
        }
      });
    },
    setTime() {
      this.time = setInterval(() => {
        if (this.timer <= 0) {
          clearInterval(this.time);
          this.isTimer = false;
          this.timer = 60;
        }
        this.timer--;
      }, 1000);
    },
  },
  watch: {
    checked(newValue, oldValue) {
      if (this.checked == true) {
        this.isChecked = false;
      } else {
        this.isChecked = true;
      }
    },
  },
  unmounted() {
    clearInterval(this.time);
  },
};
</script>

<style scoped>
.input-with-select .el-button--default {
  background-color: #fff;
  overflow: none;
  border-bottom: 1px solid #dcdfe6;
}

.el-input__inner {
  border-right: 10px solid #000 !important;
}

#login-with-phone-code {
  margin-top: 24px;
}

.agreement .agreement-text .agreement-text-link {
  color: #2684ff;
  cursor: pointer;
}
.agreement-text {
  line-height: 16px;
  font-size: 12px;
  color: #9ea2ae;
  margin-left: 5px;
}
.agreement {
  margin-top: 24px;
  font-family: PingFangSC-Regular, PingFang SC, sans-serif;
  font-weight: 400;
  line-height: 1;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: start;
  -webkit-align-items: flex-start;
  -ms-flex-align: start;
  align-items: flex-start;
  width: 100%;
  height: auto;

  word-break: break-all;
}

#login-with-phone-code {
  box-sizing: border-box;
}
.submit-login {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC, sans-serif;
  font-weight: 400;
  color: #fff;
  height: 36px;
  background: #2684ff;
  border-radius: 6px;
  cursor: pointer;
  margin-top: 18px;
}

.el-select .el-input {
  width: 130px;
}
.input-with-select .el-input-group__prepend {
  background-color: #fff;
}

.btns {
  overflow: hidden;
  font-size: 12px;
  font-family: PingFangSC-Regular, PingFang SC, sans-serif;
  font-weight: 400;
}

.btns .forget {
  color: #98989a;
  padding: 0 0 10px;
  cursor: pointer;
  line-height: 15px;
}

.fr {
  float: right;
}
</style>
